<?php
/**
 * @since   2022-02-15
 * @author  zhaoxiang <zhaoxiang051405@gmail.com>
 */

namespace App\Controller\Center;

use App\Constants\ErrorCode;
use App\Controller\AbstractController;

class BaseController extends AbstractController {
    private array $debug = [];

    /**
     * 成功的返回
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function buildSuccess(array $data = [], string $msg = '操作成功', int $code = ErrorCode::SUCCESS): array {
        $return = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
        if (env('APP_ENV', 'prod') === 'dev' && !empty($this->debug)) {
            $return['debug'] = $this->debug;
        }

        return $return;
    }

    /**
     * 错误的返回
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function buildFailed(int $code, string $msg = '操作失败', array $data = []): array {
        $return = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
        if (env('APP_ENV', 'prod') === 'dev' && !empty($this->debug)) {
            $return['debug'] = $this->debug;
        }

        return $return;
    }

    /**
     * debug参数收集
     * @param $data
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    protected function debug($data): void {
        if ($data) {
            $this->debug[] = $data;
        }
    }
}
